package com.jielin.handling.controller.policecase;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jielin.handling.Annotation.MyPower;
import com.jielin.handling.bean.AppResult;
import com.jielin.handling.entity.police.Cases;
import com.jielin.handling.entity.police.PoliceMan;
import com.jielin.handling.entity.vo.CasePicVo;
import com.jielin.handling.page.DataGrid;
import com.jielin.handling.service.police.PoliceManService;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Case;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by wangjienice on 2019-04-04.
 */
@CrossOrigin
@RestController
@RequestMapping("/policecase/")
@Slf4j
@MyPower(name = "案件详情信息模块", url = "/policecase")
public class PoliceCaseController {
    @Autowired
    PoliceManService Service;

    /**
     * 根据案件编号查询案件详情信息
     */
    @MyPower(name = "根据案件编号查询案件详情信息", url = "/selectCaseByCase")
    @RequestMapping("selectCaseByCase")
    public AppResult selectCaseByCaseId(int pageNum, int pageSize, String id) {
        log.info("selectCaseByCase query...");
        log.info("caseCode:{}", JSONObject.toJSONString(id));
        log.info("pageNum:{}", JSONObject.toJSONString(pageNum));
        log.info("pageSize:{}", JSONObject.toJSONString(pageSize));
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        List<Cases> orderList = Service.selectCaseByCaseId(pageNum, pageSize, id);
        //用PageInfo对结果进行包装
        PageInfo<Cases> pageInfo = new PageInfo<Cases>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 根据案件编号查询案件的详细信息
     */
    @MyPower(name = "根据案件编号查询案件的详细信息", url = "/selectByCaseCode")
    @RequestMapping("selectByCaseCode")
    public AppResult selectByCaseCode(String caseCode, String policeId) {
        log.info("caseCode:{}", JSONObject.toJSONString(caseCode));
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        if (StringUtil.isEmpty(caseCode)) {
            date.put("value", 0);
            appResult.setCode(-1);
            appResult.setMsg("参数为空");
            appResult.setData(date);
            return appResult;
        }
        Map map = Service.selectByCaseCode(caseCode, policeId);
        date.put("casess", map);
        appResult.setCode(200);
        appResult.setMsg("查询案件的详细信息成功");
        appResult.setData(date);

        return appResult;

    }

    /**
     * 案件类型下拉框
     *
     * @return
     */
    @MyPower(name = "获取案件类型", url = "/selectCaseType")
    @RequestMapping("selectCaseType")
    public AppResult selectCaseType() {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        List<Cases> list = Service.selectCaseType();
        date.put("list", list);
        appResult.setCode(200);
        appResult.setMsg("查询案件类型成功");
        appResult.setData(date);

        return appResult;
    }

    /**
     * 审核民警/警号   下拉框   刘嘉伟/04486
     *
     * @return unicode  单位编号
     */
    @MyPower(name = "根据单位编号查询民警/警号", url = "/selectNameAndCode")
    @RequestMapping("selectNameAndCode")
    public AppResult selectNameAndCode(String unicode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        List<PoliceMan> list = Service.selectNameAndCode(unicode);
        date.put("list", list);
        appResult.setCode(200);
        appResult.setMsg("查询民警下拉成功");
        appResult.setData(date);
        return appResult;
    }

    /**
     * @return unicode  单位编号
     */
    @MyPower(name = "根据单位编号查询单位下所有案件", url = "/selectCaseByUnitcode", isPublic = true)
    @RequestMapping("selectCaseByUnitcode")
    public AppResult selectCaseByUnitcode(@RequestBody JSONObject queryCondition) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        log.info("user query...");
        CasePicVo vo = new CasePicVo();
        vo.setQueryCondition(queryCondition);
        vo.setPage(pageNum, pageSize);
        List<CasePicVo> orderList = Service.selectCaseByUnitcode(vo);
        //用PageInfo对结果进行包装
        PageInfo<CasePicVo> pageInfo = new PageInfo<CasePicVo>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        appResult.setCode(200);
        appResult.setMsg("query success");
        appResult.setData(date);
        return appResult;

    }

}
